package com.wei.controller;

import com.wei.entity.Member;
import com.wei.entity.vo.LoginVo;
import com.wei.entity.vo.RegisterVo;
import com.wei.service.MemberService;
import com.wei.service_base.vo.BaseMemberVo;
import com.wei.service_base.vo.MemberOrderVo;
import com.wei.utils.JwtUtils;
import com.wei.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author wjx
 * @ClassName MenberController
 * @description: TODO
 * @date 2022-12-03 21:44
 * @version: 1.0
 */
@RestController
@RequestMapping("/eduucenter/member")
public class MenberController {

    @Autowired
    private MemberService memberService;

    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo) {

        String token = memberService.MemberLogin(loginVo);

        return R.ok().data("token", token);
    }

    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo) {

        memberService.memberRegister(registerVo);

        return R.ok();
    }

    @GetMapping("/getUserInfo")
    public R getUserInfo(HttpServletRequest request) {

        String id = JwtUtils.getMemberIdByJwtToken(request);

        Member member = memberService.getById(id);

        return R.ok().data("member", member);
    }

    /**
     * 根据id获取用户基本信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getUserInfoById/{id}")
    public BaseMemberVo getUserInfoById(@PathVariable String id) {

        Member member = memberService.getById(id);

        BaseMemberVo baseMemberVo = new BaseMemberVo();
        BeanUtils.copyProperties(member, baseMemberVo);

        return baseMemberVo;
    }

    @GetMapping("/getMemberOrderInfo/{id}")
    public MemberOrderVo getMemberOrderInfo(@PathVariable String id) {
        Member member = memberService.getById(id);

        MemberOrderVo memberOrderVo = new MemberOrderVo();

        BeanUtils.copyProperties(member, memberOrderVo);

        return memberOrderVo;
    }

    /**
     * 统计某一天的注册人数
     *
     * @param day 日期 格式"xxxx-xx-xx"
     * @return
     */
    @PostMapping("/countRegister/{day}")
    public Integer countRegister(@PathVariable String day) {
        Integer count = memberService.countRegisterByDay(day);

        return count;
    }
}

